#include "stm32f10x.h"                  // Device header
#include "systick.h"
#include "gpio.h"
#include "time.h"
#include "usart.h"
#include "stdio.h"
#include "oled.h"
#include "spi.h"
#include "pic.h"
#include "iic.h"
#include "bs8116.h"
#include "string.h"
#include "voice.h"
#include "rfid.h"
#include "at24cxx.h"
u8 setpassword();
u8 confirm();
u8 confirm2();
u8 Add_Card();
void Waring();
u8 Ver_Card();
u8 buff[128] = {0};
u8 flag = 1,i;
u8 Key_Num = 0;
u8 Buff[7] = {0};              //输入密码接收数组
u8 Count = 0;                  //密码长度
u8 Flag = 0;    //密码输入完成标志，0：没有输入完成，1：输入完成
u8 Pass_Word[7] ={0};     //设定密码
u8 card_id[4] = {88,23,45,56};
u8 CT[2];
u8 Card_Flag = 0;
u8 Pass_Flag = 0;
u8 qwe[4];
u8 flag3 = 0;
u8 flag4 = 0;
u8 Reset[1] = {0};
u8 Warn_flag;
int main()
{

	USART_1_Init(115200);
	IIC_Pin_Init();
	OLED_Init();
	RFID_Init();
	LED_Init();
	BS8116_Init();
	Voice_Init();
	u8 Set_Flag = 0;
	u8 key = 0;
	u8 init_flag = 1;
	u8 resetPass = 0;
	u8 resetCard = 0;
	while(1)
	{
		LED2_ON;
		if(init_flag == 1){
			OLED_Dis_String(0,40,( const unsigned char *)"第十组");
			OLED_Dis_String(4,5,( const unsigned char *)"刘思雨");
			OLED_Dis_String(4,70,( const unsigned char *)"胡阔文");
			Delay_ms(1000);
			Delay_ms(1000);
			init_flag = 0;
			OLED_Clear(0,0,127,7);
		}
		while(Card_Flag == 0 | Pass_Flag == 0)
		{
	/* 初始化显示 */
			//Init();
			OLED_Dis_String(0,20,( const unsigned char *)"2.设置密码");
			OLED_Dis_String(2,20,( const unsigned char *)"8.添加卡片");
			Key_Num = BS8116_Get_Val();
			switch(Key_Num)
		{
			case '2':Du;setpassword();OLED_Clear(0,0,127,7);break;
			case '8':Du;Add_Card();OLED_Clear(0,0,127,7);break;
		}
		Delay_ms(100);
		}
		AA:
		OLED_Dis_String(0,5,( const unsigned char *)"请按'#'输入密码");
		OLED_Dis_String(2,40,( const unsigned char *)"或刷卡");
		OLED_Dis_String(6,0,( const unsigned char *)"按'*'管理员模式");
		key = Key_Scan();
		if(key == 1){
			Voice_Send_Data(0x1e);
		}
		if(PcdRequest(PICC_REQALL,CT) == MI_OK)   //寻卡成功
		{
			Ver_Card();
			if(Warn_flag == 3){
				Voice_Send_Data(0x1f);
				Waring();
				Warn_flag = 0;
			}
		}
		Key_Num = BS8116_Get_Val();
		if(Key_Num == '#'){
			confirm();
			if(Warn_flag == 3){
						Waring();
						Warn_flag = 0;
					}
		}
		if(Key_Num == '*'){
			Voice_Send_Data(0x0c);
			if(confirm2() == 1){
				while(1){
					OLED_Dis_String(0,20,( const unsigned char *)"2.修改密码");
					OLED_Dis_String(2,20,( const unsigned char *)"8.更换卡片");
					OLED_Dis_String(4,20,( const unsigned char *)"*.退出");
					Key_Num = BS8116_Get_Val();
					switch(Key_Num)
					{
						case '2':Du;setpassword();OLED_Clear(0,0,127,7);Delay_ms(500);Voice_Send_Data(0x11);break;
						case '8':Du;Card_Flag = 0;Add_Card();OLED_Clear(0,0,127,7);Delay_ms(500);Voice_Send_Data(0x11);break;
						case '*':Du;OLED_Clear(0,0,127,7);goto AA;
					}
				}
			}
			if(Warn_flag == 3){
				Waring();
				Warn_flag = 0;
			}
		}
		
	}
}

u8 Ver_Card(){
	if(PcdAnticoll(card_id)==MI_OK)/*防冲撞成功*/
			{	if(PcdSelect(card_id)==MI_OK)//选卡成功
				{	AT24Cxx_Continuous_Read(0xa4,0xf0,qwe,1);//读卡		
					if(card_id[0] == qwe[0])
					{
						OLED_Clear(0,0,127,7);
						Delay_ms(500);
						while( GPIO_ReadInputDataBit(GPIOB,  GPIO_Pin_1));
						OLED_Dis_String(0,0,(const unsigned char *)"welcome");
						sprintf((char *)buff,":%d%d%d%d",card_id[0],card_id[1],card_id[2],card_id[3]);
						OLED_Dis_String(2,0,(unsigned char *)buff);	
						Voice_Send_Data(0x18);
						Delay_ms(500);
						//Delay_ms(1000);
						Delay_ms(1000);
						OLED_Clear(0,0,127,7);
					} 
					else{
						OLED_Clear(0,0,127,7);
						Delay_ms(500);
						while( GPIO_ReadInputDataBit(GPIOB,  GPIO_Pin_1));  
						OLED_Clear(0,0,127,7);
						OLED_Dis_String(0,0,(const unsigned char *)"Error");
						sprintf((char *)buff,"%d%d%d%d",card_id[0],card_id[1],card_id[2],card_id[3]);
						OLED_Dis_String(2,0,(unsigned char *)buff);
						Voice_Send_Data(0x0f);
						while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0));
						Delay_ms(1000);
						OLED_Clear(0,0,127,7);
						Warn_flag++;
					}
				}			
			
			}
}
u8 Add_Card(){
	OLED_Clear(0,0,127,7);
	OLED_Dis_String(0,20,( const unsigned char *)"请刷卡");
	while(Card_Flag == 0){
				if(PcdRequest(PICC_REQALL,CT) == MI_OK)   //寻卡成功
			{
				if(PcdAnticoll(card_id)==MI_OK)/*防冲撞成功*/
				{
					if(PcdSelect(card_id)==MI_OK)//选卡成功
					{
						AT24Cxx_Continuous_Read(0xa4,0xF0,qwe,1);//读取卡号
							
						AT24Cxx_Page_Write(0xa4,0xF0,card_id,4);//写进eeproom
						flag3 = 0;
						flag4 = 0;
						Reset[0] = 1;
						AT24Cxx_Page_Write(0xa4,0x24,Reset,1); 
						OLED_Clear(0,0,127,7);
						Voice_Send_Data(0x11);
						while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0));
						Delay_ms(1000);
						Card_Flag = 1 ;
					}			
			
				}
			}
		}
}
u8 setpassword(){
	char buff[10];
	OLED_Clear(0,0,127,7);
	OLED_Dis_String(0,20,( const unsigned char *)"正在设置密码");
	
	while(1)
	{
		Key_Num = BS8116_Get_Val();
		if(Key_Num !=0)
		{
			Pass_Word[Count++] = Key_Num;
			sprintf(buff,"%c",Key_Num);
			OLED_Dis_String(5,Count*8,(const unsigned char *)buff); //在液晶屏上显示按键值
			Du;
			if(Count >= 6)                            //判断输入的密码是否够6位数
			{
				Buff[Count] = '\0';                   //把输入密码接收数组变成字符串数组
				Count = 0;                            //输入密码接收计数清零
				Delay_ms(1000);
				Pass_Flag = 1;
				return 0;
			}
		}
	}
}

u8 confirm(){
	u8 i;
	u8 Clear_Flag = 0;	//再次输入密码标志
	char buff[10];
	OLED_Clear(0,0,127,7);
	OLED_Dis_String(0,20,( const unsigned char *)"请输入密码");
	while(1) 
	{
		
		Key_Num = BS8116_Get_Val();
		if( (Key_Num != 0) && (Flag == 0) )     //判断数组键盘是否有操作，并且没有完成密码输入
		{
			printf("Key_Num :%d\r\n",Key_Num);
			/* 清除上次操作的输入显示 */
			if(Clear_Flag == 1)
			{
				Clear_Flag = 0;
				OLED_Clear(0,0,127,7);
			}
			Buff[Count++] = Key_Num;//把数值键盘按键值保存到密码数组
			sprintf(buff,"%c",Key_Num);
			Du;
			OLED_Dis_String(5,Count*8,(const unsigned char *)buff); //在液晶屏上显示按键值
				Key_Num = 0;                              //数字键盘按键值清零
			if(Count >= 6)                            //判断输入的密码是否够6位数
			{
				Buff[Count] = '\0';                   //把输入密码接收数组变成字符串数组
				Count = 0;                            //输入密码接收计数清零
				Flag = 1;                             //密码输入完成标志位置1
				OLED_Clear(0,0,127,7);
			}
		}
			
			/* 输入密码判断 */
			if(Flag == 1)                                 //判断密码是否输入完成                       
			{
				Flag = 0;                                 //标志位清零
				if( strcmp((const char *)Buff,(const char *)Pass_Word) == 0)  //判断输入的密码和设定密码是否一致
				{
					Delay_ms(500);
					Voice_Send_Data(0x18);
					OLED_Dis_String(0,36,(const unsigned char *)"欢迎回家");   //显示提示信息
					Clear_Flag = 1;                                           //完成一次密码输入标志位置1
					Delay_ms(1000);
					OLED_Clear(0,0,127,7);
					return 1;
				}
				else
				{
					Delay_ms(500);
					Voice_Send_Data(0x0f);
					OLED_Dis_String(0,44,(const unsigned char *)"Error"); 					//显示提示信息
					Clear_Flag = 1;					//完成一次密码输入标志位置
					Warn_flag++;
					Delay_ms(1000);
					OLED_Clear(0,0,127,7);
					return 0;	
				}
				
				/* 接收密码数组清零 */
				for(i = 0; i < 7; i++)
				{
					Buff[i] = 0;
				}
			}
		}
}


u8 confirm2(){
	u8 i;
	u8 Clear_Flag = 0;	//再次输入密码标志
	char buff[10];
	OLED_Clear(0,0,127,7);
	OLED_Dis_String(0,20,( const unsigned char *)"请输入密码");
	while(1) 
	{
		
		Key_Num = BS8116_Get_Val();
		if( (Key_Num != 0) && (Flag == 0) )     //判断数组键盘是否有操作，并且没有完成密码输入
		{
			printf("Key_Num :%d\r\n",Key_Num);
			/* 清除上次操作的输入显示 */
			if(Clear_Flag == 1)
			{
				Clear_Flag = 0;
				OLED_Clear(0,0,127,7);
			}
			Buff[Count++] = Key_Num;//把数值键盘按键值保存到密码数组
			sprintf(buff,"%c",Key_Num);
			Du;
			OLED_Dis_String(5,Count*8,(const unsigned char *)buff); //在液晶屏上显示按键值
				Key_Num = 0;                              //数字键盘按键值清零
			if(Count >= 6)                            //判断输入的密码是否够6位数
			{
				Buff[Count] = '\0';                   //把输入密码接收数组变成字符串数组
				Count = 0;                            //输入密码接收计数清零
				Flag = 1;                             //密码输入完成标志位置1
				OLED_Clear(0,0,127,7);
			}
		}
			
			/* 输入密码判断 */
			if(Flag == 1)                                 //判断密码是否输入完成                       
			{
				Flag = 0;                                 //标志位清零
				if( strcmp((const char *)Buff,(const char *)Pass_Word) == 0)  //判断输入的密码和设定密码是否一致
				{
					Delay_ms(500);
					Voice_Send_Data(0x11);
					OLED_Dis_String(0,36,(const unsigned char *)"密码正确");   //显示提示信息
					Clear_Flag = 1;                                           //完成一次密码输入标志位置1
					Delay_ms(1000);
					OLED_Clear(0,0,127,7);
					return 1;
				}
				else
				{
					Delay_ms(500);
					Voice_Send_Data(0x0f);
					OLED_Dis_String(0,44,(const unsigned char *)"Error"); 					//显示提示信息
					Clear_Flag = 1;					//完成一次密码输入标志位置
					Warn_flag++;
					Delay_ms(1000);
					OLED_Clear(0,0,127,7);
					return 0;	
				}
				
				/* 接收密码数组清零 */
				for(i = 0; i < 7; i++)
				{
					Buff[i] = 0;
				}
			}
		}
}

void Waring(){
	u8 i = 0;
	Voice_Send_Data(0x1f);
	while(i < 5){
		LED1_ON;
		Delay_ms(300);
		LED1_OFF;
		Delay_ms(300);
		i++;
	}
}




